package com.bootdo.provider.controller;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import com.alibaba.fastjson.JSON;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.utils.*;
import com.bootdo.provider.domain.dto.GoodsExcel;
import com.bootdo.provider.service.ProviderInfoService;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.provider.domain.GoodsInfoDO;
import com.bootdo.provider.service.GoodsInfoService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 商品表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-04-20 11:24:50
 */
 
@Controller
@RequestMapping("/provider/goodsInfo")
public class GoodsInfoController {
	@Autowired
	private GoodsInfoService goodsInfoService;
	@Autowired
	private ProviderInfoService providerInfoService;
	@Autowired
	private UserService userService;
	
	@GetMapping()
	@RequiresPermissions("provider:goodsInfo:goodsInfo")
	String GoodsInfo(@RequestParam(required = false) Long providerId,Model model){

		System.out.println("providerId:"+providerId);
		model.addAttribute("providerId",providerId);
		return "provider/goodsInfo/goodsInfo";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("provider:goodsInfo:goodsInfo")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<GoodsInfoDO> goodsInfoList = goodsInfoService.list(query);
		int total = goodsInfoService.count(query);
		PageUtils pageUtils = new PageUtils(goodsInfoList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	//@RequiresPermissions("provider:goodsInfo:add")
	String add(){
	    return "provider/goodsInfo/add";
	}

	@GetMapping("/edit/{goodsId}")
	//@RequiresPermissions("provider:goodsInfo:edit")
	String edit(@PathVariable("goodsId") Long goodsId,Model model){
		GoodsInfoDO goodsInfo = goodsInfoService.get(goodsId);
		model.addAttribute("goodsInfo", goodsInfo);
	    return "provider/goodsInfo/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	//@RequiresPermissions("provider:goodsInfo:add")
	public R save( GoodsInfoDO goodsInfo){

		if (goodsInfo.getProviderId()==null)
			return R.error(CodeUtils.NO_PROVIDER,"没有添加供应商");

		goodsInfo.setGoodsProvider(providerInfoService.get(goodsInfo.getProviderId()).getProviderSname());

	if(goodsInfoService.save(goodsInfo)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	//@RequiresPermissions("provider:goodsInfo:edit")
	public R update( GoodsInfoDO goodsInfo){
		goodsInfoService.update(goodsInfo);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	//@RequiresPermissions("provider:goodsInfo:remove")
	public R remove( Long goodsId){
		if(goodsInfoService.remove(goodsId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	//@RequiresPermissions("provider:goodsInfo:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] goodsIds){
		goodsInfoService.batchRemove(goodsIds);
		return R.ok();
	}


	/*      我的商品     */


	@GetMapping("my")
	//@RequiresPermissions("provider:goodsInfo:goodsInfo")
	String GoodsInfoMy(Model model){
		UserDO userDO=ShiroUtils.getUser();

		model.addAttribute("providerId",userDO.getProviderId());
		//System.out.println("userDO:"+ JSON.toJSON(userDO));
		return "provider/goodsInfo/my/goodsInfo";
	}


	@ResponseBody
	@GetMapping("/list/my")
	//@RequiresPermissions("provider:goodsInfo:goodsInfo")
	public PageUtils listMy(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<GoodsInfoDO> goodsInfoList = goodsInfoService.list(query);
		int total = goodsInfoService.count(query);
		PageUtils pageUtils = new PageUtils(goodsInfoList, total);
		return pageUtils;
	}

	@GetMapping("/add/my")
		//@RequiresPermissions("provider:goodsInfo:add")
	String addMy(){
		return "provider/goodsInfo/my/add";
	}
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save/my")
	//@RequiresPermissions("provider:goodsInfo:add")
	public R saveMy( GoodsInfoDO goodsInfo){
		UserDO userDO=ShiroUtils.getUser();
		goodsInfo.setProviderId(userDO.getProviderId());
		goodsInfo.setGoodsProvider(providerInfoService.get(goodsInfo.getProviderId()).getProviderSname());

		if(goodsInfoService.save(goodsInfo)>0){
			return R.ok();
		}
		return R.error();
	}
	@GetMapping("/edit/my/{goodsId}")
		//@RequiresPermissions("provider:goodsInfo:edit")
	String editMy(@PathVariable("goodsId") Long goodsId,Model model){
		GoodsInfoDO goodsInfo = goodsInfoService.get(goodsId);
		model.addAttribute("goodsInfo", goodsInfo);
		return "provider/goodsInfo/my/edit";
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update/my")
	//@RequiresPermissions("provider:goodsInfo:edit")
	public R updateMy(GoodsInfoDO goodsInfo){
		UserDO userDO=ShiroUtils.getUser();
		goodsInfo.setProviderId(userDO.getProviderId());
		goodsInfoService.update(goodsInfo);
		return R.ok();
	}
	@ResponseBody
	@PostMapping("/upload")
	R upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

		String fileName = file.getOriginalFilename();
		fileName = FileUtil.renameToUUID(fileName);
		FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/" + fileName, new Date());
		ImportParams params = new ImportParams();
		params.setTitleRows(1);
		params.setHeadRows(1);
		long start = new Date().getTime();
		List<GoodsExcel> list = ExcelImportUtil.importExcel(
				new File(PoiPublicUtil.getWebRootPath("/海顺sku.xls")),
				GoodsExcel.class, params);
		return R.error();
	}
}
